package com.mmaingenieria.GestorFacturacion.controller;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmaingenieria.GestorFacturacion.beans.Usuario;
import com.mmaingenieria.GestorFacturacion.model.DAOFactory;
import com.mmaingenieria.GestorFacturacion.model.interfaces.InterfaceUsuarioDAO;

/**
 * Servlet implementation class AjaxUserExistServlet
 */
public class AjaxUserExistServlet extends InitServlet implements Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see InitServlet#InitServlet()
     */
    public AjaxUserExistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doGet(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
		try{
			//recojemos valor pasado por Ajax
			String nombre = request.getParameter("nombre");
			
			//obtenemos usuario de la base datos
			DAOFactory factoriaMySql = DAOFactory.getDAOFactory(1);
			InterfaceUsuarioDAO interfaceUsuario = factoriaMySql.getInterfaceUsuarioDAO();
			Usuario user = interfaceUsuario.getUsuarioByName(nombre);
			
			if ( user.getId() == Usuario.NEW_USER ){
				response.getWriter().write("NO existe");
			}else{
				response.getWriter().write("existe");
			}
		}catch(Exception e){
			e.printStackTrace();
		}	
		
		
	}

}
